<template>
  <div class="dashboard-container">
    <div v-if="is_news===1" class="dashboard-message">
      <div class="message-icon">
        <svg-icon icon-class="message" />
      </div>
      <p v-show="message_status===1" class="message-text">您之前的群发消息审核已通过</p>
      <p v-show="message_status===2" class="message-text">您之前的群发消息审核未通过</p>
      <span class="view pointer" @click="handleCloseMessage">点击查看</span>
    </div>
    <div
      class="dashboard-draft"
    >
      <div class="draft-title">
        <div>草稿箱</div>
        <el-button type="primary" @click="handleCreateDynamic">新建动态</el-button>
      </div>
      <div
        v-loading="loadingDraft"
        class="draft-content"
        element-loading-text="加载中"
        element-loading-spinner="el-icon-loading"
      >
        <ul>
          <li v-for="(item,index) in draftList" class="content-item">
            <img :src="item.show_image" alt="封面" class="item-img" width="100%" height="100%">
            <div class="item-title">{{ item.title }}</div>
            <div class="item-edit show">
              <div class="pointer" style="margin-right: 60px;" @click="edit(item.dynamic_id)">
                <p style="font-size: 14px;">
                  <svg-icon icon-class="edit" />
                </p>
                编辑
              </div>
              <div class="pointer" style="margin-right: 6px;" @click="send(item)">
                <p style="font-size: 14px;">
                  <svg-icon icon-class="send" />
                </p>
                发送
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div
      :loading="loadingDialog"
      class="dynamic-content"
      element-loading-text="加载中"
      element-loading-spinner="el-icon-loading">

      <el-dialog
        :visible.sync="dialogVisible"
        :show-close="false"
      >
        <Preview :preview-content="previewContent" />
      </el-dialog>
    </div>
    <div
      class="dashboard-dynamic"
    >
      <div class=" dynamic-title">
        <p>已发动态</p>
      </div>
      <div
        :loading="loadingDynamic"
        class="dynamic-content"
        element-loading-text="加载中"
        element-loading-spinner="el-icon-loading"
      >
        <ul >
          <li v-for="(list,index) in dynamicList.data" :key="index" class="content-item">
            <div class="item-date">{{ list.date }}</div>
            <ul style="width: 100%;">
              <li v-for="(item,index) in list.list" :key="index" class="item">
                <img :src="item.show_image" alt="封面" class="item-img" width="100%" height="100%">
                <div class="item-title ">
                  <div class="title ">
                    {{ item.title }}
                  </div>
                  <div class="evaluation">
                    <span style="padding-right: 20px;">
                      <svg-icon icon-class="visible" />
                      {{ item.look_number }}
                    </span>
                    <span style="padding-right: 20px;">
                      <svg-icon icon-class="comment" />
                      {{ item.comment_number }}
                    </span>
                  </div>
                </div>
                <div class="item-edit show">
                  <div class="pointer" style="margin-right: 60px;" @click="handlePreview(item.dynamic_id)" >
                    <p style="font-size: 14px;">
                      <svg-icon icon-class="view" />
                    </p>
                    查看
                  </div>
                  <div class="pointer" style="margin-right: 6px;" @click="handleDeleteDynamic(item.dynamic_id)">
                    <p style="font-size: 14px;">
                      <svg-icon icon-class="delete" />
                    </p>
                    删除
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getServiceDraftList, getOneDraftInfo, getServiceMessage, getServiceDynamicList, saveServiceDynamic, delServiceDynamic, closeServiceMessage } from '@/api/dynamic'
import Preview from '@/components/Preview'
export default {
  name: 'Dashboard',
  components: {
    Preview
  },
  data() {
    return {
      dialogVisible: false,
      loadingDialog: false,
      loadingDraft: true,
      loadingDynamic: true,
      previewContent: {
        title: 'sjkdjflskfl',
        content: '<h1>jsljflj</h1>',
        author: '了降落伞的'
      },
      is_news: 1,
      message_status: 1,
      draftList: [],
      dynamicList: []
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getServiceMessage().then(response => {
        const data = response
        if (data.status === 200) {
          this.is_news = data.data.is_news
          this.message_status = data.data.message_status
        }
      })
      this.getServiceDynamic()
      this.getServiceDraft()
    },
    getServiceDynamic() {
      this.loadingDynamic = true
      getServiceDynamicList().then(response => {
        if (response.status === 200) {
          this.dynamicList = response.data
          this.loadingDynamic = false
        }
      })
    },
    getServiceDraft() {
      this.loadingDynamic = true
      getServiceDraftList().then(response => {
        if (response.status === 200) {
          this.draftList = response.data
          this.loadingDraft = false
        }
      })
    },
    edit(id) {
      this.$router.push({ path: '/draft/index', query: { id: id, status: 1 }})
    },
    send(item) {
      console.log(item)
      item.status = 0
      saveServiceDynamic(item).then(response => {
        if (response.status === 200) {
          this.getServiceDraft()
          this.$message({
            message: '发送审核成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发送审核失败',
            type: 'failure'
          })
        }
      })
    },
    // 查看动态
    handlePreview(id) {
      this.dialogVisible = true
      this.loadingDialog = true
      getOneDraftInfo(id).then(response => {
        if (response.status === 200) {
          this.previewContent = response.data
          this.loadingDialog = false
          console.log(this.previewContent,'preview')
        }
      })
    },
    // 删除动态
    handleDeleteDynamic(id) {
      this.$confirm('此操作将永久删除该动态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delServiceDynamic(id).then(response => {
          if (response.status === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getServiceDynamic()
          } else {
            this.$message({
              type: 'danger',
              message: response.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCreateDynamic() {
      this.$router.push({ path: '/draft/index', query: { status: 1 }})
    },
    handleCloseMessage() {
      closeServiceMessage().then(res => {
        this.$router.push('/check/index')
      })
    }
  }
}
</script>
<style lang="scss" rel="stylesheet">
  .dashboard-container {
    .el-dialog {
      width: 428px;
      height: 857px;
      border-radius: 46px;
      margin-top: 2vh !important;
      background: url("../../assets/img/mobile-bg.png");
      .el-dialog__header {
        padding: 0;
      }
    }
    .el-dialog__body {
      overflow: hidden;
      border: none;
    }
    .el-dialog__headerbtn {
      top:0;
      right: 0;
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  .item-title {
    white-space: nowrap;
overflow:hidden;
    text-overflow:ellipsis;
  }
  .item-img {
    flex-shrink: 0;
  }
  .dashboard {
    &-container {
      margin: 30px;
    }
    &-message {
      background: rgba(255, 255, 255, 1);
      border: 1px solid rgba(233, 233, 233, 1);
      padding-left: 33px;
      display: flex;
      .message-icon {
        font-size: 20px;
        height: 50px;
        line-height: 50px;
      }
      .message-text {
        height: 19px;
        font-size: 14px;
        font-family: MicrosoftYaHei;
        color: rgba(51, 51, 51, 1);
        line-height: 19px;
        margin-right: 16px;
        margin-left: 13px;
      }
      .view {
        height: 50px;
        font-size: 14px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(35, 144, 255, 1);
        line-height: 50px;
      }
    }
    &-draft {
      background-color: #fff;
      margin-top: 30px;
      padding: 23px 30px 32px 30px;
      .draft-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 33px;
      }
      .draft-content {
        .content-item {
          display: flex;
          margin-bottom: 30px;
          position: relative;
          &:hover {
            background-color: #f7f7f7;
            .item-edit {
              display: flex;
            }
          }
          .item-img {
            width: 160px;
            height: 90px;
          }
          .item-title {
            margin-right: 300px;
            margin-left: 20px;
            padding-top: 8px;
          }
          .item-edit {
            position: absolute;
            top: 0;
            right:0;
            padding: 10px 40px;
            text-align: center;
            display: none;
            /*display: flex;*/
            justify-content: space-between;
            width: 200px;
            font-size: 12px;
            font-family: MicrosoftYaHei;
            color: rgba(102, 102, 102, 1);
            line-height: 16px;
          }
        }
      }
    }
    &-dynamic {
      background-color: #fff;
      margin-top: 30px;
      padding: 23px 30px 32px 30px;
      .dynamic-title {
        display: flex;
        justify-content: space-between;
        padding-bottom: 33px;
      }
      .dynamic-content {
        .content-item {
          display: flex;
          margin-bottom: 30px;
          .item {
            display: flex;
            margin-bottom: 30px;
            position: relative;
            &:hover {
              background-color: #f7f7f7;
              .item-edit {
                display: flex;
              }
            }
            .item-edit {
              position: absolute;
              z-index: 666;
              top: 0;
              right:0;
              padding: 10px 40px;
              text-align: center;
              display: none;
              /*display: flex;*/
              justify-content: space-between;
              width: 200px;
              font-size: 12px;
              font-family: MicrosoftYaHei;
              color: rgba(102, 102, 102, 1);
              line-height: 16px;
            }
          }
          .item-date {
            flex-shrink: 0;
            padding-right: 30px;
            text-align: right;
            width: 92px;
            font-size: 14px;
            font-family: MicrosoftYaHei;
            color: rgba(102, 102, 102, 1);
          }
          .item-img {
            width: 160px;
            height: 90px;
          }
          .item-title {
            flex-grow: 1;
            margin-right: 200px;
            margin-left: 20px;
            padding-top: 8px;
            .title {
              padding-bottom: 35px;
              font-size: 16px;
              font-family: MicrosoftYaHei;
              color: rgba(51, 51, 51, 1);
              line-height: 21px;
            }
          }
        }
      }
    }
  }
</style>
